package com.power.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @Description security框架认识的用户对象
 * @Author dingchunlin
 * @Date 2024/07/11 19:06
 * @Version 1.0.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SecurityUser implements UserDetails {

    //商城后台管理的相关属性

    private Long userId;

    private String username;

    private String password;

    private Integer status;

    private Long shopId;

    private String loginType;

    private Set<String> perms = new HashSet<>();

    //商城购物系统会员相关属性
    private String openid;


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.loginType +  this.userId;
    }

    public Set<String> getPerms() {
        HashSet<String> finalPerms = new HashSet<>();
        // 循环遍历权限集合
        perms.forEach(perm -> {
            // 判断是否有逗号
            if (perm.contains(",")) {
                // 逗号分隔，添加到集合
                String[] permArr = perm.split(",");
                for (String p : permArr) {
                    finalPerms.add(p);
                }
            }else {
                finalPerms.add(perm);
            }
        });
        return finalPerms;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.status == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.status == 1;
    }

    @Override
    public boolean isEnabled() {
        return this.status == 1;
    }
}
